package com.suning.sawp.member.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.dto.customer.CustshopFrequency;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.dto.storeman.Category;
import com.suning.sawp.intf.esb.ShopFrequencyService;
import com.suning.sawp.member.constant.MemberConstants;
import com.suning.sawp.member.dao.StoreExtensionDao;
import com.suning.sawp.member.service.LabelService;
import com.suning.sawp.member.service.StoreInquiryService;
import com.suning.sawp.member.vo.CateExpandDetailVo;
import com.suning.sawp.member.vo.CustLabelVo;
import com.suning.sawp.member.vo.EmpExpandDetailVo;
import com.suning.sawp.member.vo.MemberExtensionCateVo;
import com.suning.sawp.member.vo.MemberExtensionClientVo;
import com.suning.sawp.member.vo.MemberExtensionEmpVo;
import com.suning.sawp.member.vo.MemberExtensionStatisVo;
import com.suning.sawp.member.vo.StoreExpandDetailVo;
import com.suning.sawp.service.util.AESUtils;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.service.util.DateUtils;

/**
 * 
 * 门店询价未购服务实现 <br>
 *
 * @author 14060886 祝翔龙
 */
@Service
public class StoreInquiryServiceImpl implements StoreInquiryService {

	@Autowired
	private ShopFrequencyService shopFrequencyService;
	@Autowired
	private StoreExtensionDao storeExtensionDao;

	@Autowired
	private LabelService labelService;

	@Override
	public StoreExpandDetailVo queryInquiryDetailByStore(String storeCode, String dateType) {
		StoreExpandDetailVo result = new StoreExpandDetailVo();
		// 查询品类外拓信息
		List<MemberExtensionCateVo> cateList = storeExtensionDao.selectExpandCateInfoByStore(null, storeCode, dateType, MemberConstants.INQUIRY_NOT_BUY);
		if (CollectionUtils.isEmpty(cateList)) {
			cateList = new ArrayList<MemberExtensionCateVo>();
		} else {
			for (MemberExtensionCateVo cate : cateList) {
				cate.setCateName(ConfigConstants.DEFAULT_CATE_MAP.get(cate.getCateCode()));
			}
		}
		result.setCateList(cateList);
		// 查询统计外拓信息
		MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByStore(null, storeCode, dateType, MemberConstants.INQUIRY_NOT_BUY);
		if (null == statistics) {
			statistics = new MemberExtensionStatisVo();
		}
		setTime(statistics, dateType);
		result.setStatistic(statistics);
		return result;
	}

	@Override
	public CateExpandDetailVo queryInquiryDetailByCate(StoreManDto storeMan, String cateCode, String dateType) {
		CateExpandDetailVo result = new CateExpandDetailVo();
		List<Category> categories = storeMan.getCategories();
		// 未维护品类直接返回
		if (CollectionUtils.isEmpty(categories)) {
			return result;
		}
		String storeCode = storeMan.getStoreCode();
		List<MemberExtensionEmpVo> employeeList = new ArrayList<MemberExtensionEmpVo>();
		if (StringUtils.isEmpty(cateCode)) {
			for (Category category : categories) {
				String categoryCode = category.getCategoryCode();
				// 查询店员外拓信息(品类)
				employeeList = storeExtensionDao.selectExpandEmpInfoByCate(null, storeCode, categoryCode, dateType, MemberConstants.INQUIRY_NOT_BUY);
				if (CollectionUtils.isNotEmpty(employeeList)) {
					cateCode = categoryCode;
					break;
				}
			}
			if (CollectionUtils.isEmpty(employeeList)) {
				cateCode = categories.get(0).getCategoryCode();
			}
		} else {
			// 查询店员外拓信息(品类)
			employeeList = storeExtensionDao.selectExpandEmpInfoByCate(null, storeCode, cateCode, dateType, MemberConstants.INQUIRY_NOT_BUY);
		}

		result.setEmployeeList(employeeList);
		result.setCates(generateCategorys(categories, cateCode));
		MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByCate(null, storeCode, cateCode, dateType, MemberConstants.INQUIRY_NOT_BUY);
		if (null == statistics) {
			statistics = new MemberExtensionStatisVo();
		}
		setTime(statistics, dateType);
		result.setStatistic(statistics);
		return result;
	}

	@Override
	public EmpExpandDetailVo queryInquiryDetailByEmp(String storeCode, String employeeId, String dateType, String pageNo) {
		EmpExpandDetailVo result = new EmpExpandDetailVo();
		List<MemberExtensionClientVo> clientList = storeExtensionDao.selectExpandClientInfoByEmp(null, storeCode, employeeId, null, MemberConstants.INQUIRY_NOT_BUY, dateType, Integer.valueOf(pageNo),
				MemberConstants.PAGE_SIZE);
		if (CollectionUtils.isEmpty(clientList)) {
			clientList = new ArrayList<MemberExtensionClientVo>();
		} else {
			for (MemberExtensionClientVo client : clientList) {
				client.setMobile(getPhone(client.getMobile()));
				client.setCreateTime(DateUtils.getNextMonthStr(client.getCreateTime(), DateUtils.DEFAULT_DATE_PATTERN, 0));
				// 查询标签列表
				List<CustLabelVo> custLabels = labelService.queryFiveLabelByCustId(Long.parseLong(client.getCustId()));
				client.setCustLabels(custLabels);
			}
		}
		result.setCustomerList(clientList);
		if (MemberConstants.ONE_PAGE.intValue() == Integer.parseInt(pageNo)) {
			MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByEmp(null, storeCode, employeeId, null, MemberConstants.INQUIRY_NOT_BUY, dateType);
			if (null == statistics) {
				statistics = new MemberExtensionStatisVo();
			}
			setTime(statistics, dateType);
			result.setStatistic(statistics);
		}
		return result;
	}

	@Override
	public EmpExpandDetailVo queryInquiryDetailByEmp4DY(String storeCode, String employeeId, String startDateStr, String endDateStr, String pageNo) {
		EmpExpandDetailVo result = new EmpExpandDetailVo();
		Date startDate;
		Date endDate;
		if (StringUtils.isEmpty(startDateStr) && StringUtils.isEmpty(endDateStr)) {
			// 默认七天内
			startDate = DateUtils.getNowDateShort(DateUtils.DEFAULT_DATE_PATTERN, DateUtils.getNextDay(new Date(), -6));
			endDate = DateUtils.getNowDateShort(DateUtils.DEFAULT_DATE_PATTERN, DateUtils.getNextDay(new Date(), 1));
			startDateStr = DateUtils.getDateStrByFormat(startDate, DateUtils.DEFAULT_DATE_PATTERN);
			endDateStr = DateUtils.getDateStrByFormat(endDate, DateUtils.DEFAULT_DATE_PATTERN);
		} else {
			startDate = DateUtils.getDateByStr(startDateStr);
			endDate = DateUtils.getNextDay(DateUtils.getDateByStr(endDateStr), 1);
		}
		result.setStartDate(startDateStr);
		result.setEndDate(endDateStr);
		List<MemberExtensionClientVo> clientList = storeExtensionDao.selectInquiryDetailByEmp4DY(storeCode, employeeId, startDate, endDate, Integer.valueOf(pageNo), MemberConstants.PAGE_SIZE);
		List<String> custList = new ArrayList<String>();
		if (CollectionUtils.isEmpty(clientList)) {
			clientList = new ArrayList<MemberExtensionClientVo>();
		} else {
			for (MemberExtensionClientVo client : clientList) {
				client.setMobile(getPhone(client.getMobile()));
				client.setCreateTime(DateUtils.getFormatDay(client.getCreateTime(), DateUtils.DEFAULT_TIME_PATTERN, DateUtils.DEFAULT_DATE_PATTERN));
				custList.add(client.getCustNo());
			}
		}

		if (CollectionUtils.isNotEmpty(custList)) {
			// 查询会员首购复购接口
			Map<String, String> frequencyList = shopFrequencyService.queryShopFrequency(custList);
			for (MemberExtensionClientVo client : clientList) {
				client.setBuyStatus(processBuyStatus(client.getCustNo(), frequencyList));
				// 查询标签列表
				List<CustLabelVo> custLabels = labelService.queryFiveLabelByCustId(Long.parseLong(client.getCustId()));
				client.setCustLabels(custLabels);
			}
		}
		result.setCustomerList(clientList);
		return result;
	}

	private void setTime(MemberExtensionStatisVo statistics, String dateType) {
		if (dateType.equals(MemberConstants.INQUIRE_TODAY)) {
			statistics.setTime(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_DATE_PATTERN));
		} else if (dateType.equals(MemberConstants.INQUIRE_YESTERDAY)) {
			statistics.setTime(DateUtils.getDateStrByFormat(DateUtils.getNextDay(new Date(), -1), DateUtils.DEFAULT_DATE_PATTERN));
		} else {
			statistics.setTime(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_MONTH_PATTERN));
		}
	}

	// 生成品类数据, 返回给前端
	private List<Map<String, String>> generateCategorys(List<Category> categories, String cateCode) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (int i = 0; i < categories.size(); i++) {
			Map<String, String> map = new HashMap<String, String>(3);
			String cCode = categories.get(i).getCategoryCode();
			map.put("cateCode", cCode);
			map.put("cateName", categories.get(i).getCategoryName());
			map.put("checked", cateCode.equals(cCode) ? "1" : "0"); // 设置默认选中值
			list.add(map);
		}
		return list;
	}

	private String getPhone(String phone) {
		if (StringUtils.isNotEmpty(phone)) {
			return AESUtils.encrypt(phone, ConfigConstants.AES_KEY);
		}
		return StringUtils.EMPTY;
	}

	private String processBuyStatus(String custNo, Map<String, String> frequencyList) {
		if (StringUtils.isBlank(custNo) || frequencyList.isEmpty()) {
			return CustshopFrequency.DealType.NOT_MEMBER;
		} else {
			// 获取客户购物状态对象
			CustshopFrequency custshopFrequency = DJStringUtils.getDealType(frequencyList.get(custNo));
			return custshopFrequency.getDealType();
		}
	}

}
